package com.yuanduan.mplus.view;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;


import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.entity.ConsultEntity;
import com.yuanduan.mplus.presenter.ConsultListViewAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by cj on 2016/6/6.
 * Email:781806481@qq.com
 * 老版本资讯入口
 */
public class ConsultActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private Toolbar toolbar;
    private ListView listView;
    private SwipeRefreshLayout swipeRefreshLayout;

    private List<ConsultEntity> lists=new ArrayList<ConsultEntity>();

    private int internetNum=0;
    private int internetTargetNum=3;
    private ConsultListViewAdapter consultListViewAdapter;
    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallbackWithIndex httpInfoCallback=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            //将result的数据填入
            //发送网络请求，请求图片
            Log.d("ConsultActivity","infocallback onsucceed"+index);
            ConsultEntity entity=new ConsultEntity();
            entity.setContent("hello world"+index);
            entity.setTitle("hello");
            entity.setTime("world");
            entity.setSortId(index);
            entity.setContentUrl("http://www.baidu.com");
            entity.setBitmapUrl("http://avatar.csdn.net/6/B/9/1_easyer2012.jpg");
            lists.add(entity);
            iHttp.httpBitmapGetWithIndex(entity.getBitmapUrl(),bitmapHttpInfoCallback,lists.indexOf(entity));
        }

        @Override
        public void onFailed(Object result, int index) {
            Log.d("ConsultActivity","infocallback onfailed"+index);
        }
    };
    private IHttp.HttpInfoCallbackWithIndex bitmapHttpInfoCallback=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            Log.d("ConsultActivity","bitmapinfo onsucceed"+index);
            internetNum++;
            for(int i=0;i<lists.size();i++){
                if(lists.get(i).getSortId()==index){
                    lists.get(i).setBitmap((Bitmap) result);
                }
            }
            if(internetNum==internetTargetNum){
                swipeRefreshLayout.setRefreshing(false);
                consultListViewAdapter.setDatalists(lists);
                lists.clear();
                internetNum=0;
            }
        }

        @Override
        public void onFailed(Object result, int index) {

        }
    };

    @Override
    protected void findId() {
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        listView= (ListView) findViewById(R.id.consult_lv);
        swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.consult_swiperl);
    }

    @Override
    protected void logic() {
        toolbar.setTitle("资讯");
        toolbar.setTitleTextColor(Color.BLACK);
        toolbar.setNavigationIcon(R.mipmap.back_arrow_black);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        swipeRefreshLayout.setOnRefreshListener(this);

        consultListViewAdapter=new ConsultListViewAdapter(this);
        listView.setAdapter(consultListViewAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ConsultEntity entity= (ConsultEntity) consultListViewAdapter.getItem(position);
                Bundle bundle=new Bundle();
                Log.d("ConsultActivity","url"+((ConsultEntity) consultListViewAdapter.getItem(position)).getContentUrl());
                bundle.putString("contentUrl",entity.getContentUrl());
                openActivity(ConsultDetailActivity.class,bundle);
            }
        });

        internetNum=0;
        internetTargetNum=2;
        iHttp.httpInfoPostWithIndex("http://www.baidu.com",new HashMap<String, String>(),httpInfoCallback,0);
        iHttp.httpInfoPostWithIndex("http://www.baidu.com",new HashMap<String, String>(),httpInfoCallback,1);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_consult;
    }


    @Override
    public void onRefresh() {
        internetNum=0;
        internetTargetNum=3;
        iHttp.httpInfoPostWithIndex("http://www.baidu.com",new HashMap<String, String>(),httpInfoCallback,0);
        iHttp.httpInfoPostWithIndex("http://www.baidu.com",new HashMap<String, String>(),httpInfoCallback,1);
        iHttp.httpInfoPostWithIndex("http://www.baidu.com",new HashMap<String, String>(),httpInfoCallback,2);
    }
}
